const reqInterface = require('../../../../api/models')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    houseList: [],
    list: [],
  },

  onShow() {
    let self = this;
    self.getHouse()
    self.getUser()
  },

  getUser() {
    let self = this;
    reqInterface.GetRooms({page: 1, limit: 100,user_id: wx.getStorageSync('user_id')}).then(res => {
      if(res.length > 0) {
        self.setData({
          list: res
        })
      }
    })
  },

  getHouse() {
    let self = this;
    reqInterface.GetHouses({
      page: 1,
      limit: 100,
      name: '',
      id: 0
    }).then(res => {
      self.setData({
        houseList: res
      })
    })
  },

  houseIdValue(e) {
    let self = this;
    self.setData({
      'info.house_id': e.detail.val
    })
    self.getRoom(e.detail.val)
  },

  getRoom(val) {
    let self = this;
    reqInterface.GetRooms({
      page: 1,
      limit: 100,
      house_id: val
    }).then(res => {
      self.setData({
        list: res
      })
    })
  },

  bind(e) {
    let self = this;
    wx.showModal({
      title: '是否要绑定此房屋？',
      confirmText: '是',
      showCancel: true,
      success(res) {
        if (res.confirm) {
          reqInterface.PostRoom({
            id: e.currentTarget.dataset.item.id,
            user_id: wx.getStorageSync('user_id'),
            house_id: e.currentTarget.dataset.item.house_id,
            name: e.currentTarget.dataset.item.name,
          }).then(res => {
            self.getUser()
          })
        }
      }
    })
  }

})